<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="icon" href="imags/11.jpg">
</head>

<body style="background:url(/imags/1.jpg); background-size:100% 100% ; background-attachment: fixed">
    <div class="container">
        <div class="row align-items-center">
            <div class="col"></div>
            <div class="col">
                <div class="alert alert-danger text-center invisible" role="alert" style="width: 42rem;">
                    <strong>提示：</strong>
                    <font id="msgId">提示框</font>
                </div>
            </div>
            <div class="col"></div>
        </div>
        <div class="row align-items-center" style="height: 30rem;">
            <div class="col">

            </div>
            <div class="col">
                <div class="card shadow rounded" style="width: 42rem;opacity: 0.7;">
                    <div class="card-body align-items-center">
                        <h2 class="align-middle" style="margin-left: 17rem;
                        margin-bottom: 2rem;">登录</h2>
                        <form id="loginForm">

                            <div class="form-group row">
                                <label class="col-2" for="exampleInputEmail1"
                                    style="margin-left: 6rem;margin-top: 2px;">用户名：</label>
                                <input type="text" class="form-control col-6" id="username" name="username"
                                    aria-describedby="emailHelp" placeholder="请输入用户名" style="margin-left: -1rem;">
                            </div>
                            <div class="form-group row">
                                <label class="col-2" for="exampleInputPassword1"
                                    style="margin-left: 5rem;margin-top: 2px;padding-left: 2rem;">密码：</label>
                                <input type="password" class="form-control col-6" id="password" name="password"
                                    placeholder="请输入密码">
                            </div>
                            <div class="form-group form-check row" style="margin-left: 11rem;">

                                <input type="checkbox" class="form-check-input" id="exampleCheck1">
                                <label class="form-check-label" for="exampleCheck1">记住我</label>
                              

                            </div>
                            <button type="button" class="btn btn-primary" style=" margin-left: 10rem;"
                                onclick="jump()">登录</button>
                            <button type="button" class="btn btn-primary" style=" margin-left: 10rem;"
                                onclick="register()">注册</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col">

            </div>
        </div>
    </div>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        function editpassword(){
           window.location.href='/lostpassword'
        }


        function register() {
            window.location.href = '/register';
        }
        function jump() {
            let uid = $('#username').val();
            let pwd = $('#password').val();

            if (uid.length > 0 && pwd.length > 0) {
                axios({
                    method: 'post',
                    url: '/loginDone',
                    data: $('#loginForm').serialize(),
                }).then(function (res) {
                    res = res.data;
                    if (res.code === 200) {
                        console.log(res);
                        if (res.user === 'admin') {
                            window.location.href = '/';
                        }
                        else {
                            location.href = '/qestion?id=' + res.id
                        }
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空');
            }
        }


        function alert_fn(msg) {
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>
</body>

</html>